package org.jeecg.modules.demo.weixin.util;

import lombok.SneakyThrows;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.jeecg.modules.demo.weixin.entity.MerchantPay;

import javax.net.ssl.SSLContext;
import java.io.FileInputStream;
import java.security.KeyStore;

public class WeChatUtil {

    /**
     * Post请求+证书
     * @param url  微信申请退款接口链接
     * @param entity  微信申请退款接口的参数
     */
    @SneakyThrows
    public static HttpResponse httpPost(String url, String entity, MerchantPay merchantPay,Integer type) {
        String keystorePath = "";
        String keystorePassword = "";
        if (type.equals(0)) {
//            keystorePath = "D:/xxx/1570120791.p12";
            keystorePath = PayConstants.API_REFUND_PATH;
            keystorePassword = PayConstants.MCH_ID_SERVICE;
        }
        if (type.equals(1)) {
            keystorePath = merchantPay.getMchSsl();
            keystorePassword = merchantPay.getMchId();
        }

        //注意PKCS12证书 是从微信商户平台->账号设置->API安全 中下载的
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        //加载本地的证书进行https加密传输,keystorePath是证书的绝对路径
        FileInputStream instream = new FileInputStream(keystorePath);
        try{
            //设置证书密码,keystorePassword:下载证书时的密码，默认密码是你的mchid
            keyStore.load(instream, keystorePassword.toCharArray());
        } finally {
            instream.close();
        }

        //java 主动信任证书
        //keystorePassword:下载证书时的密码，默认密码是你的mchid
        SSLContext sslcontext = SSLContexts.custom()
                .loadKeyMaterial(keyStore, keystorePassword.toCharArray()).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslcontext, null, null,
                SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        //CloseableHttpClient 加载证书来访问https网站
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        try {
            // 设置响应头信息，发送post请求
            HttpPost httpPost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(5000)
                    .setConnectTimeout(5000)
                    .setConnectionRequestTimeout(5000)
                    .build();
            httpPost.setConfig(requestConfig);
            httpPost.setEntity(new StringEntity(entity, "UTF-8"));
            httpPost.setHeader("Accept", "*/*");
            httpPost.setHeader("Content-type", "application/xml");
            HttpResponse resp = httpclient.execute(httpPost);
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
